package com.ruoyi.web.controller.dj;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.dj.domain.*;
import com.ruoyi.dj.service.IModelCalcService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;

@RestController
@RequestMapping("/dj/model")
@Slf4j
@Api(tags = "多模型计算与预报-高精度模型计算")
public class ModelCalcController {

    @Autowired
    private IModelCalcService modelCalcService;



    @ApiOperation("大气密度分布模拟计算")
    @PostMapping("/global")
    public R<ModelCalcGlobalVO> airCal(@RequestBody ModelCalcGlobalParam param) {
        log.info("进入测试");
        try {
        ModelCalcGlobalVO vo = modelCalcService.globalCal(param);
            return R.ok(vo);
        } catch (RuntimeException e) {
            return R.fail(e.getMessage());
        }

    }



    @ApiOperation("大气密度分布模拟计算结果下载")
    @PostMapping("/global/download")
    public void airDownload(@Validated @RequestBody ModelCalcGlobalParam param, HttpServletResponse response) {
        String file = modelCalcService.globalDownload(param);
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, new File(file).getName());
            FileUtils.writeBytes(file, response.getOutputStream());
        }catch (Exception e) {
            log.error("下载失败",e);
        }finally {
            new File(file).delete();
        }
    }


    @ApiOperation("轨道大气密度模拟计算")
    @PostMapping("/track")
    public R<TrackCalVO> trackCal(@RequestBody ModelCalcTrackParam param) {
        try {
            TrackCalVO vo = modelCalcService.trackCal(param);
            return R.ok(vo);
        } catch (Exception e) {
            log.error("轨道大气密度模拟计算异常",e);
            return R.fail(e.getMessage());
        }

    }


    @ApiOperation("轨道计算结果下载")
    @PostMapping("/track/download")
    public void download(@Validated @RequestBody ModelCalcTrackParam param, HttpServletResponse response) {
        try {
            String file = modelCalcService.trackDownload(param);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, new File(file).getName());
            FileUtils.writeBytes(file, response.getOutputStream());
            new File(file).delete();
        }catch (Exception e) {
            log.error("下载失败",e);
        }
    }


    @ApiOperation("全球垂直分布")
    @PostMapping("/vertical")
    public R<ModelCalcGlobalVO> vertical(@RequestBody ModelCalcVerticalParam param) {
        try {
            ModelCalcGlobalVO vo = modelCalcService.verticalCal(param);
            return R.ok(vo);
        } catch (Exception e) {
            log.error("全球垂直分布计算异常",e);
            return R.fail(e.getMessage());
        }

    }

    @ApiOperation("全球垂直分布结果下载")
    @PostMapping("/vertical/download")
    public void verticalDownload(@Validated @RequestBody ModelCalcVerticalParam param, HttpServletResponse response) {
        try {
            String file = modelCalcService.verticalDownload(param);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, new File(file).getName());
            FileUtils.writeBytes(file, response.getOutputStream());
            new File(file).delete();
        }catch (Exception e) {
            log.error("下载失败",e);
        }
    }

}
